﻿using UnityEngine;
using System;
using System.Runtime.InteropServices;
using AOT;
using UnityEngine.UI;
using System.Text;


using System.Collections;
using Assets.MatchVS;
using UnityEngine.SceneManagement;


public class MatchVS : MonoBehaviour
{


    static MatchVS instance;

    int mDebugMode = 1;//default is first debug
    //public static int DevelopID = 1;
    //public static int UserID = 49;
    //public static int GameID = 200695 ;//102001;//100011; kof97
    public static int GameVersion = 1;
    //public static string Token = "TLCPZWQWCEWIUPSHYQINPJYMWNAUAVOP";
    //public volatile static bool s_bGotUserInfo = false;
    //public volatile static int s_iUserID = 0;
    public static String mCheckOrderID = "144446378700219";
    /// <summary>
    /// 支付商品ID
    /// </summary>
    public static int[] goodsids= {0,1,2};
    public static String[] goodsNames = { "充个1分的", "充个10块大的", "土豪包,宝刀屠龙只要1000" };
    Dialog dialog;
    void Awake()
    {

        instance = this;
        //初始化Loom脚本
        Loom.QueueOnMainThread(() =>
        {
        });
        //获取需要监听的按钮对象设置这个按钮的监听，指向本类的ButtonClick方法中。NGUI
        initBtn("BtnBack");
        initBtn("BtnClear");
        initBtn("BtnHttp");
        initBtn("BtnEnvSwitch");
        initBtn("BtnClean");
        initBtn("BtnUser");

        initBtn("BtnBattle");
        initBtn("BtnMoney");

        initBtn("BtnLoginGuest");
        initBtn("BtnEntryLobby");

        initBtn("BtnSign");
        initBtn("BtnGift");
        initBtn("BtnBottomBar1");
        initBtn("BtnBottomBar2");
        initBtn("BtnBottomBar3");
        initBtn("BtnBottomBar4");
        initBtn("BtnBottomBar5");
        initBtn("BtnCancle");

        
        dialog = Dialog.create("Dialog");
        instance = this;

        src[0] = new Http.Product();
        src[1] = new Http.Product();
        src[0].productID = 144;
        src[0].count = 2;
        src[0].countDecimal = 50;
        src[1].productID = 145;
        src[1].count = 1;
        src[1].countDecimal = 50;

        dst[0] = new Http.Product();
        dst[0].productID = 146;
        dst[0].count = 1;
    }
    int updateCount = 0;
    void Update() {
        updateCount++;

    }
    //计算按钮的点击事件
    void ButtonClick(GameObject button)
    {
        Debug.Log("GameObject " + button.name);
        switch (button.name)
        {
            case "BtnEnvSwitch":
                Http.setEnvironment(Http.getEnvironment() == 0 ? 1 : 0);
                UILabel label = (button.GetComponentInChildren<UILabel>());
                label.text = Http.getEnvironment() == 0 ? "release" : "debug";
                break;

            case "BtnClear":
                Clean();
                break;
            case "BtnHttp":
                SceneManager.LoadScene("ScenceBaseScroller");
                break;
            case "BtnBack":
                SceneManager.LoadScene("ScenceLobby");
                break;
            case "BtnUser":
                Http.hashGet(this, rsp, appkey, token, userID, gameID, key);
                Http.hashSet(this, rsp, appkey, token, userID, gameID, key, keyValue);
                Http.getUserBattleInfo(this, rsp, appkey, token, userID, gameID);
                Http.getUserInfoList(this, rsp, new int[] { 32, 23 });
                break;
            case "BtnBattle":

                Http.battleFields(this, rsp, gameID, versionID);
                Http.brokeSubsidy(this, rsp, gameID, userID, productID);
                Http.getItemRank(this, rsp, gameID, item, 15);//15 is pagesize
                Http.getSystemTime(this, rsp);
                Http.getUserRank(this, rsp, gameID, item, userID, token);
                break;
            case "BtnMoney":
                Http.safeExchange(this, rsp, appkey, token, userID, gameID, src, 2, dst, 1);
                Http.safeSpend(this, rsp, appkey, token, userID, gameID, src, 2);
                Http.award(this, rsp, appkey, token, userID, gameID, awardID);
                Http.awardInfo(this, rsp, appkey, token, userID, gameID, awardID);
                Http.awardRec(this, rsp, appkey, token, userID, gameID, awardID);
                Http.loginSubsidy(this, rsp, appkey, gameID, userID, token);
                Http.offlineSyncProduct(this, rsp, appkey, token, userID, gameID, 100, 500);//100 is gold coin,500 is count
                break;

                case "BtnLoginGuest":
                    break;
                case "BtnEntryLobby":
                SceneManager.LoadScene("ScenceLobby");
                break;

                case "BtnSign":
                    break;
                case "BtnGift":
                    dialog.show();
                    dialog.setText("Sign签名");
                    break;
                case "BtnBottomBar1":
                    break;
                case "BtnBottomBar2":
                    break;        
                case "BtnBottomBar3":
                    SceneManager.LoadScene("ScenceTopList");
                break;        
                case "BtnBottomBar4":
                    break;        
                case "BtnBottomBar5":
                    break;
                case "BtnCancle":
                    dialog.hide();
                break;
                
            default:
                break;
        }

    }
    
    protected void initBtn( String BtnName)
    {
        GameObject obj = GameObject.Find(BtnName);
        if(obj!=null) {
        var uIEventListener = UIEventListener.Get(obj).onClick = ButtonClick;
        }
    }
    public void Clean()
    {
        UILabel messageBox = GameObject.Find("MessageBox").GetComponent<UILabel>();
        messageBox.text = "  ";
    }




    void OnGUI() {
        GUILayout.Label(" Version :  " + defaultVersion);
        GUILayout.Label(" VsZone @" + "2016/6/15");
       
    }



    int defaultVersion;

    public static MatchVS getInstance()
    {
        return instance;
    }
    public void showMessage(String msg)
    {
        //Debug.Log("showMessage:" + msg);
        UILabel messageBox = GameObject.Find("MessageBox").GetComponent<UILabel>();
        string strText = messageBox.text;
        strText += "\r\n" + msg;
        messageBox.text = strText;

    }
   

    //测试用例
    delegate void TestCase();

    Http.OnRsp rsp = (text, err) => {
        MatchVS.getInstance().showMessage(text + err);
    };

    void TestHttpApi() {

        TestCase safeExchange = () => {
            Http.safeExchange(this, rsp, appkey, token, userID, gameID, src, 2, dst, 1);
        };
        TestCase safeSpend = () => {
            Http.safeSpend(this, rsp, appkey, token, userID, gameID, src, 2);
        };
        Http.award(this,rsp,appkey,token,userID,gameID,awardID);
        Http.awardInfo(this,rsp,appkey,token,userID,gameID,awardID);
        Http.awardRec(this, rsp, appkey, token, userID, gameID, awardID);
        Http.battleFields(this,rsp,gameID,versionID);
        Http.brokeSubsidy(this,rsp,gameID,userID,productID);
        Http.getItemRank(this,rsp,gameID, item, 15);//15 is pagesize
        Http.getSystemTime(this,rsp);
        Http.getUserRank(this,rsp,gameID, item, userID,token);
        Http.hashGet(this,rsp,appkey,token,userID,gameID,key);
        Http.hashSet(this, rsp, appkey,token,userID,gameID,key, keyValue);
        Http.loginSubsidy(this,rsp,appkey,gameID,userID,token);
        Http.offlineSyncProduct(this, rsp, appkey,token, userID, gameID, 100,500);//100 is gold coin,500 is count
        Http.getUserBattleInfo(this, rsp, appkey, token, userID, gameID);
        Http.getUserInfoList(this,rsp,new int [] { 32, 23 });
        ArrayList testCases = new ArrayList();
        testCases.Add(safeExchange);
        testCases.Add(safeSpend);
        doTestCases(testCases);
    }
    void doTestCases(ArrayList testCases) {
        foreach(TestCase item in testCases) {
            item();
        }
    }


    public static int userID = 32;//调用SDK登录接口获得
    public const int gameID = 200695;//102000;//官网注册帐号获得
    public static String token = "HLAYXDDYGAQYELLWEOVULVPSIIWZVSWL";//调用SDK登录接口获得
    public static String appkey = "910b0cf4782549c38da31333e69ddc3d";//官网注册帐号获得
    public static String item = "extend_0";//官网后台配置获得,用户属性字段名称

    public static String keyValue = "888";//自定义储存的值
    
    public static int key = 888;//自定义储存的值
    public static Http.Product[] src = new Http.Product[2];
    public static Http.Product[] dst = new Http.Product[1];
    public static int awardID = 1;
    public static int versionID = 1;
    public static int productID = 101;

    
}
